package cn.sy.util;

import cn.sy.exception.GlobalCustomException;
import cn.sy.exception.GlobalErrorCodeEnum;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertUtil {

    //手机的正则表达式
    private static final Pattern CHINA_PATTERN_PHONE = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");


    /**
     * --------------------------------------------------------
     * 手机号断言
     * --------------------------------------------------------
     **/
    public static void isPhone(String phone, GlobalErrorCodeEnum globalErrorCodeEnum) {
        isNotEmpty(phone, GlobalErrorCodeEnum.COMM_PHONE_IS_NULL_ERROR);
        Matcher m = CHINA_PATTERN_PHONE.matcher(phone);
        if (!m.matches()) {
            throw new GlobalCustomException(globalErrorCodeEnum);
        }
    }


    /**
     * --------------------------------------------------------
     * 断言 不为空，如果为空，抛异常
     * --------------------------------------------------------
     **/
    public static void isNotEmpty(String text, GlobalErrorCodeEnum globalErrorCodeEnum) {
        if (text == null || text.trim().length() == 0) {
            throw new GlobalCustomException(globalErrorCodeEnum);
        }
    }


    /**
     * --------------------------------------------------------
     * 断言对象为空
     * --------------------------------------------------------
     **/
    public static void isNull(Object obj, GlobalErrorCodeEnum globalErrorCodeEnum) {
        if (obj != null) {
            throw new GlobalCustomException(globalErrorCodeEnum);

        }
    }

    public static void isNotNull(Object obj, GlobalErrorCodeEnum globalErrorCodeEnum) {
        if (obj == null) {
            throw new GlobalCustomException(globalErrorCodeEnum);
        }
    }

    /**
     * --------------------------------------------------------
     * 断言false,如果为true,我报错
     * --------------------------------------------------------
     **/
    public static void isFalse(boolean isFalse, GlobalErrorCodeEnum globalErrorCodeEnum) {
        if (isFalse) {
            throw new GlobalCustomException(globalErrorCodeEnum);


        }
    }

    public static void isTrue(boolean isTrue, GlobalErrorCodeEnum globalErrorCodeEnum) {
        if (!isTrue) {
            throw new GlobalCustomException(globalErrorCodeEnum);

        }
    }


    /**
     * --------------------------------------------------------
     * 断言两个字符串一致
     * --------------------------------------------------------
     **/
    public static void isEquals(String s1, String s2, GlobalErrorCodeEnum globalErrorCodeEnum) {
        isNotEmpty(s1, GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        isNotEmpty(s2, GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        if (!s1.equals(s2)) {
            throw new GlobalCustomException(globalErrorCodeEnum);


        }
    }

    public static void isEqualsTrim(String s1, String s2, GlobalErrorCodeEnum globalErrorCodeEnum) {
        isNotEmpty(s1, GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        isNotEmpty(s2, GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        if (!s1.trim().equals(s2.trim())) {
            throw new GlobalCustomException(globalErrorCodeEnum);


        }
    }

    public static void isEqualsIgnoreCase(String s1, String s2, GlobalErrorCodeEnum globalErrorCodeEnum) {
        isNotEmpty(s1, GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        isNotEmpty(s2, GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        if (!s1.trim().equalsIgnoreCase(s2.trim())) {
            throw new GlobalCustomException(globalErrorCodeEnum);

        }
    }

    public static void isImCode(Object imageCodeValue, String imageCode, GlobalErrorCodeEnum commVerificationIsIncorrectError) {
        isNotEmpty((String) imageCodeValue,GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        isNotEmpty(imageCode,GlobalErrorCodeEnum.SERVICE_PARAM_IS_NULL);
        if (!imageCodeValue.toString().equalsIgnoreCase(imageCode)) {
            throw new GlobalCustomException(commVerificationIsIncorrectError);
        }
    }


    public static void IsNotLessTime(Long nowTime, Long time, Long intervaltime, GlobalErrorCodeEnum repeatoperationError) {
        if (nowTime - time < intervaltime) {
            throw new GlobalCustomException(repeatoperationError);
        }
    }
}
